package com.haojin.java;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 * @author .29.
 * @create 2022-04-06 21:11
 */
/*
Collection接口中声明的方法的测试

向Collection接口的实现类的对象中添加数据obj时，要求obj所在的类重写equals()
 */
public class CollectionTest {
    @Test
    public void test1() {
        Collection coll = new ArrayList();

        coll.add(123);
        coll.add(435);
        coll.add(false);
        coll.add(new String("Tom"));
        coll.add(999);
        coll.add(new Person("Hao",19));

        //1.contains(Object obj)
        //我们在判断是会调用obj对象锁在类的equals()。
        boolean contains = coll.contains(123);
        System.out.println(contains);
        System.out.println(coll.contains(new String("Tom")));

        //2.containsAll(Collection coll1):判断形参coll1中的所有元素是否都存在与当前集合中
        Collection coll1 = Arrays.asList(123,4352);
        System.out.println(coll.containsAll(coll1));
    }

    @Test
    public void test2(){
        Collection coll = new ArrayList();

        coll.add(123);
        coll.add(435);
        coll.add(false);
        coll.add(new String("Tom"));
        coll.add(999);
        coll.add(new Person("Hao",19));

        //3.remove(Object obj):从当前集合中移除obj元素
        coll.remove(123);
        coll.remove(999);
        System.out.println(coll);

        //4.removeAll(Collection coll1):从当前集合中移除coll1中所有的元素
        Collection coll1 = Arrays.asList("Tom",435);
        coll.removeAll(coll1);
        System.out.println(coll);
    }

    @Test
    public void test3(){
        Collection coll = new ArrayList();
        coll.add(123);
        coll.add(435);
        coll.add(false);
        coll.add(new String("Tom"));
        coll.add(999);
        coll.add(new Person("Hao",19));
        //5.retainAll(Collection coll1):获取当前集合和coll1集合的交集，并返回给当前集合
//        Collection coll1 = Arrays.asList(123,999,false);
//        coll.retainAll(coll1);
//        System.out.println(coll);

        //6.equals(Object obj):判断当前集合和形参集合元素是否相同
        Collection coll1 = new ArrayList();
        coll1.add(123);
        coll1.add(435);
        coll1.add(false);
        coll1.add(new String("Tom"));
        coll1.add(999);
        coll1.add(new Person("Hao",19));
        System.out.println(coll.equals(coll1));
    }

    @Test
    public void test4(){
        Collection coll = new ArrayList();
        coll.add(123);
        coll.add(435);
        coll.add(false);
        coll.add(new String("Tom"));
        coll.add(999);
        coll.add(new Person("Hao",19));

        //7.hashCode():返回当前对象的哈希值
        System.out.println(coll.hashCode());

        //8.toArray():集合--->数组
        Object[] objects = coll.toArray();
        for (int i = 0; i < objects.length; i++) {
            System.out.println(objects[i]);

        }
        //拓展：数组--->集合:调用Arrays类的静态方法asList()
        List<String> list = Arrays.asList(new String[]{"ABC", "BAC", "HH"});
        System.out.println(list);

        //iterator():返回iterator接口的实例，用于遍历集合数组。放在IteratorTest.java中测试

    }

}
